package com.github.thinwonton.springboot.showcase.mybatis.service;

import com.github.thinwonton.springboot.showcase.mybatis.dao.AccountDao;
import com.github.thinwonton.springboot.showcase.mybatis.domain.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

@Service
public class AccountService {

    @Autowired
    private AccountDao accountDao;

    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void changeMoney(Long id, BigDecimal increased) {
        Account account = accountDao.selectByPrimaryKey(id);
        account.setMoney(account.getMoney().add(increased));
        accountDao.updateByPrimaryKeySelective(account);
    }


    public void createAccount() {
        Account account = new Account();
        account.setMoney(new BigDecimal(0));
        accountDao.insert(account);
    }
}
